﻿using HslCommunication.Profinet.Siemens;
using HslCommunication;
using NetCore8Demo.Models.PLC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using NetCore8Demo.Models.Communication.SerialPortFolder;

namespace NetCore8Demo.ViewModels.PLC
{
    public class PlcTool
    {
        PlcReadAndWriteDemo _plc;

        public void Init(PlcType plcType)
        {

        }

        private PlcInfo _plcInfo = new PlcInfo();

        public PlcTool(PlcInfo PlcInfo) 
        {
            _plcInfo = PlcInfo;
            switch (PlcInfo.PlcType)
            {
                case PlcType.基恩士Binary:
                    _plc = new KeyencePlcBinary();
                    break;
                case PlcType.三菱MelsecMcNet:
                    _plc = new MelsecMcNetPlc();
                    break;
                case PlcType.西门子300:
                case PlcType.西门子400:
                case PlcType.西门子1200:
                case PlcType.西门子1500:
                case PlcType.西门子200Smart:
                    _plc = new SiemensPlc(PlcInfo.PlcType);
                    break;

                default:
                    _plc = new PlcReadAndWriteDemo();
                    break;
            }
        }


        public OperateResult ConnPlc()
        {
            OperateResult operateResult = new OperateResult();
            if (!_plcInfo.IsInUse)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC未启用";
                return operateResult;
            }
            if (_plcInfo.IsConnected) 
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC已连接";
                return operateResult;
            }

            operateResult = _plc.ConnPlc(_plcInfo.Ip, _plcInfo.Port);
            _plcInfo.IsConnected = operateResult.IsSuccess;
            return operateResult;
        }

        public OperateResult ClosePlc()
        {
            if (!_plcInfo.IsInUse)
            {
                OperateResult operateResult = new OperateResult();
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC未启用";
                return operateResult;
            }

            return _plc.ClosePlc();
        }


        public OperateResult ReadAll()
        {
            List<PlcAddressInfo> readList = StaticPlc.PlcAddressInfoList.Where(plcInfo => plcInfo.PlcInfoId == _plcInfo.Id).ToList();
            if (!_plc.InitFlag)
            {
                OperateResult operateResult = new OperateResult();
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC未连接";
                return operateResult;
            }

            return _plc.ReadAll(readList);
        }

        public OperateResult WritePlcValue(int enumIndex, string value)
        {

            OperateResult operateResult = new OperateResult();
            if (!_plc.InitFlag)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC未连接";
                return operateResult;
            }

            //if (enumIndex >= _plcAddressInfoList.Count)
            //{
            //    return false;
            //}

            //if (_plcAddressInfoList[enumIndex].State == PlcStateType.停用)
            //{
            //    return false;
            //}
            return operateResult;
        }


    }
}
